﻿using System.ComponentModel;
using Microsoft.Practices.Prism.Regions;
using VS.Web.Model;
using VS.Infrastructure;
using Microsoft.Practices.Unity;

namespace VS.MainModule.Views
{
    public class AdminViewModel : INotifyPropertyChanged, INavigationAware
    {

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region Fields
        private bool _authenticationProceeding;
        #endregion

        #region Properties

        [Dependency]
        public IRegionManager RegionManager { get; set; }

        #endregion

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (!WebContext.Current.User.IsAuthenticated)
            {
                if (_authenticationProceeding)
                {
                    _authenticationProceeding = false;
                    navigationContext.NavigationService.RequestNavigate("/main");
                }
                else
                {
                    _authenticationProceeding = true;
                    navigationContext.NavigationService.RequestNavigate("/login");
                }
            }
            else
            {
                
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            navigationContext.NavigationService.Region.RegionManager.Regions.Remove(RegionNames.AdminDocumentRegion);            
        }
    }
}
